package com.flex.client;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flex.manager.MessageManager;
import com.flex.model.DataModel;
import com.flex.model.MessageModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

/**
 * 
 * Servlet implementation classTickCacheServlet
 * 
 */

public class MessageSend extends HttpServlet {

	private static final long serialVersionUID = 1L;

	// 线程

	private static FeedThread thread;
	private static String mapId;
	

	/**
	 * 
	 * @see HttpServlet#HttpServlet()
	 * 
	 */

	public MessageSend() {

		super();

		// TODO Auto-generated constructor stub

	}

	/**
	 * 
	 * @see HttpServlet#doGet(HttpServletRequestrequest, HttpServletResponse
	 *      response)
	 * 
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// TODO Auto-generated method stub

		String cmd = request.getParameter("cmd");
		mapId = request.getParameter("mapId");

		if (cmd.equals("start"))

		{
			start();
		}

		if (cmd.equals("stop"))

		{

			stop();

		}

		System.out.println("----------mapId-==="+mapId);
		response.getWriter().write(cmd);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		super.doPost(request, response);

	}

	// 初始化

	public void init() throws ServletException {

		super.init();

	}

	public void destroy() {

		super.destroy();

	}

	// 启动监听

	public void start() {

		System.out.println(".................start....");
		if (thread == null)

		{
			thread = new FeedThread();
			thread.start();
		}
		System.out.println("start!!!");

	}

	// 停止监听

		public void stop() {
			if(thread!=null){
				thread.running = false;
				thread = null;
			}
		}
	


	// 循环发送消息进程

	public static class FeedThread extends Thread

	{
		public boolean running = true;
		MessageManager messageManager = new MessageManager();
        public DataModel dataModel = new DataModel();
	  
		public void run() {
			System.out.println("........FeedThread.........run....=");
			// 总是无法获取msgBroker,web.xml需要有MessageBrokerServlet的配置信息
			MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
			String clientID = UUIDUtils.createUUID();
			IData pdata = new DataMap();
			while (running) {
					List<MessageModel> dataList = messageManager.getObjectList(pdata);
					AsyncMessage msg = new AsyncMessage();
					msg.setDestination("tick-data-feed");
					msg.setHeader(AsyncMessage.SUBTOPIC_HEADER_NAME, "tick");
					msg.setClientId(clientID);
					msg.setMessageId(UUIDUtils.createUUID());
					msg.setTimestamp(System.currentTimeMillis());
					//塞对象信息
					dataModel.setStaffList(dataList);
					dataModel.setIsFlag("Y");
					
					msg.setBody(dataModel);
					msgBroker.routeMessageToService(msg, null);
					if(dataList!=null){
							System.out.println("前台推送.....="+dataList.size()+"条记录!");
					}
					try{
						Thread.sleep(2000);
					}catch (InterruptedException e){
						e.printStackTrace();
					}
			}

		}
		

	}

}
